<?php

namespace App\Models\Traits;
use Cache;
use App\Models\ColorAverage;
use App\Jobs\GetPictuerAverageJobs;
trait PictuerAverageHelper
{
    public function setAverage($pictuer)
    {
          if (!$pictuer->color_average_id) {
            if ($color = $pictuer->rgb) {
                $color_average = ColorAverage::firstOrCreate(['color' => $color]);
                $pictuer->update(['color_average_id' => $color_average->id]);
                $color_average->increment('pictuer_count');
            }
          }
    }
    public function getAverage($pictuer)
    {
      if (! $pictuer->rgb) {
        dispatch(new GetPictuerAverageJobs([['id' => $pictuer->id, 'path' => $pictuer->path, 'filename' => $pictuer->filename]]));
      }
    }
}
